package pixelwaks.plugins.modes;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.util.List;

import pixelwaks.core.ISprite;
import pixelwaks.core.impl.Game;
import pixelwaks.core.impl.Player;
import pixelwaks.core.impl.Target;
import pixelwaks.core.impl.Weapon;
import pixelwaks.ctrl.ControlFactory;
import pixelwaks.ctrl.impl.PlayerCtrl;
import pixelwaks.plugins.GameMode;

import common.core.Tile;
import common.util.Calculate;

public class NewAndImprovedTargetMode extends GameMode {

	public NewAndImprovedTargetMode() {
		
		ISprite sprite = new Target(100,100);
		ISprite sprite1 = new Target(200,300);
		ISprite sprite2 = new Target(400,630);
		Game.getInstance().addSprite(sprite);
		Game.getInstance().addSprite(sprite1);
		Game.getInstance().addSprite(sprite2);
	}
	
	@Override
	public void otherHitByExplosion(Point p, int strength) {
		
		explosionHitTarget(p, strength);
	}

	@Override
	public boolean isGameOver() {
		
		List<Weapon> weapons = Game.getInstance().getCurrentPlayer().getWeapons();
		
		int counter = 0;
		
		for(Weapon w: weapons) {
			counter += w.getNumberOfShots();
		}
		
		boolean b = counter == 0;
		
		if(b)
			setGameOverInfo("You got a score of " + (int) Game.getInstance().getCurrentPlayer().getStatus() + "p");
		
		return b;
	}

	@Override
	public boolean projectileHitSomething(Point p) {
		
		int projectileXpos = p.x;
		int projectileYpos = p.y;		
		
		for(ISprite sprite: Game.getInstance().getISprites()) {
			
			int spriteXpos = sprite.getPosition().x;
			int spriteYpos = sprite.getPosition().y;
			int height = sprite.getImage().getHeight();
			int width = sprite.getImage().getWidth();
			Raster raster = sprite.getImage().getRaster();
			int[] array = null;

			if(projectileXpos > spriteXpos && projectileXpos < spriteXpos + width
				&& projectileYpos > spriteYpos && projectileYpos < spriteYpos + height) {
				
				int imgX = projectileXpos - spriteXpos;
				int imgY = projectileYpos - spriteYpos;
				
				if(raster.getPixel(imgX, imgY, array)[3] != 0)
					return true;		
			}
		}
		return false;
	}

	private void explosionHitTarget(Point p, int strength) {
		
		int radius = strength * Tile.TILE_SIZE + (Tile.TILE_SIZE / 2);
		ISprite target = Game.getInstance().getISprites().get(0);
		BufferedImage img = target.getImage();
		int height = img.getHeight();
		int width = img.getWidth();
		Raster raster = img.getRaster();
		int[] array = null;

		for (int y = 0; y < height; y++)
			for (int x = 0; x < width; x++)
				if (Calculate.isInBlastArea(p.x, p.y, target.getPosition().x + x,
						target.getPosition().y + y, radius))
					if (raster.getPixel(x, y, array)[3] != 0) {
						PlayerCtrl pctrl = (PlayerCtrl) ControlFactory.getObserver(ControlFactory.PLAYER_CTRL);
						Player player = Game.getInstance().getCurrentPlayer();
						pctrl.setStatus(player, player.getStatus()+0.02);
					}		
	}
}
